
#include "head.h"


int main(int argc,char * argv[])
{
    if(argc != 2)
    {
        printf("Usage: tempc <command>\n");
        printf("command:\n");
        printf("            start   (start daemon)\n");
        printf("            stop    (stop daemon)\n");
        printf("            lon     (turn on led lights)\n");
        printf("            loff    (turn off led lights)\n");
        printf("To disbale led light, create /root/light_off file\n");
        return 0;
    }
    else if(argc ==2)
    {
        if(strcmp(argv[1],"start") == 0)
        {
            if(access(PID_FILE,F_OK) == 0)
            {
                printf("The program has already started\n");
                return -1;
            }
            system("/bin/tempcd");
            return 0;
        }
        if(access(PID_FILE,F_OK) != 0)
        {
            printf("The program may not start\n");
            return -1;
        }
        int fd_pid = open(PID_FILE,O_RDONLY);
        char buf[MAX_SIZE];
        read(fd_pid,buf,MAX_SIZE);
        long int pid = atoi(buf);

        if(strcmp(argv[1],"stop") == 0)
            kill(pid,SIGINT);
        if(strcmp(argv[1],"loff") == 0)
            kill(pid,SIGUSR1);
        if(strcmp(argv[1],"lon") == 0)
            kill(pid,SIGUSR2);
        return 0;
    }
}